
package com.amado.course35Concurrent;

import java.util.Random;
import java.util.concurrent.CountDownLatch;

/**
 * CountDownLatch 一个同步辅助类，在完成一组正在其他线程中执行的操作之前，它允许一个或多个线程一直等待。
 * 测试10个线程并发
 */
public class S01CountDownLatchTest2 {

    public static void main(String[] args) throws InterruptedException {

        CountDownLatch startSignal = new CountDownLatch(3);
        //相当于运动动陆续上场
        for (int i = 0; i < 3; i++) {
            new Thread(() -> {
                try {
                    int sleepTime = new Random().nextInt(1000);
                    System.out.println("休眠:"+sleepTime);
                    Thread.sleep(sleepTime);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                //起跑线，全部等待
                startSignal.countDown();
                //接到信息，全部开始跑
                System.out.println(" 2 " + System.currentTimeMillis());

            }).start();
        }
        System.out.println("等待开始");
        //发出信号，
        startSignal.await();
        System.out.println("开始执行");
    }
}
